package ru.budget.mobile.ui.main

import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.foundation.layout.*
import androidx.compose.ui.text.TextStyle
import ru.budget.mobile.ui.components.*
import androidx.compose.ui.text.font.*
import androidx.compose.foundation.*
import androidx.compose.material3.*
import ru.budget.mobile.ui.theme.*
import androidx.compose.runtime.*
import androidx.compose.ui.unit.*
import androidx.compose.ui.*
import androidx.compose.ui.platform.LocalView
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.androidx.compose.koinViewModel
import org.koin.compose.KoinApplication
import org.koin.core.logger.Level
import ru.budget.mobile.R
import ru.budget.mobile.di.dataModule
import ru.budget.mobile.di.databaseModule
import ru.budget.mobile.di.domainModule
import ru.budget.mobile.di.viewModelsModule
import ru.budget.mobile.ui.base.BudgetViewModel

private val budgetViewModel @Composable get() = koinViewModel<BudgetViewModel>()

@Composable
@Preview(
    showBackground = true,
    device = "id:Xiaomi POCO M5s"
)
private fun MainScreenPreview() {
    val context = LocalView.current.context.applicationContext
    KoinApplication(
        application = {
            androidLogger(Level.DEBUG)
            androidContext(context)
            modules(
                databaseModule,
                viewModelsModule,
                domainModule,
                dataModule
            )
        },
        content = {
            MainScreen(
                viewModel = budgetViewModel
            )
        }
    )
}

val visiblyActionMenu = mutableStateOf(false)

@Composable
fun MainScreen(viewModel: BudgetViewModel) {
    val state = viewModel.stateFlow.collectAsState().value

    Scaffold(
        modifier = Modifier.background(color = black),
        topBar = {
            ToolBar(
                title = "Главная",
                actions = { Balance(sum = state.balance) },
                color = white1
            )
        },
        floatingActionButton = {
            Column(
                content = {
                    FAB(
                        icon = R.drawable.add,
                        onClick = { visiblyActionMenu.value = true },
                        visibly = !visiblyActionMenu.value
                    )
                },
                horizontalAlignment = Alignment.End
            )
        },
        content = {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(it)
                    .padding(
                        start = 16.dp,
                        end = 16.dp,
                        top = 16.dp
                    )
                    .verticalScroll(rememberScrollState()),
                verticalArrangement = Arrangement.spacedBy(
                    space = 16.dp,
                    alignment = Alignment.Top
                ),
                horizontalAlignment = Alignment.Start,

                content = {
                    CoinCard(
                        title = "Траты",
                        today = state.consumptionState.sumToday,
                        week = state.consumptionState.sumWeek,
                        month = state.consumptionState.sumMonth
                    )
                    CoinCard(
                        title = "Доходы",
                        today = state.incomeState.sumToday,
                        week = state.incomeState.sumWeek,
                        month = state.incomeState.sumMonth
                    )
                }
            )
        },
        containerColor = white1
    )
}

@Composable
private fun CoinCard(
    title: String,
    today: Double,
    week: Double,
    month: Double
) {
    Card(
        modifier = Modifier
            .fillMaxWidth(),
        colors = CardColors(
            containerColor = white,
            contentColor = white,
            disabledContentColor = white,
            disabledContainerColor = white
        ),
        content = {
            Column(
                modifier = Modifier
                    .padding(
                        start = 24.dp,
                        top = 20.dp,
                        end = 24.dp,
                        bottom = 12.dp
                    ),
                content = {
                    TitleCard(title = title)
                    ItemCard(title = "Сегодня", sum = today)
                    Rectangle()
                    ItemCard(title = "Неделя", sum = week)
                    Rectangle()
                    ItemCard(title = "Месяц", sum = month)
                }
            )
        },
        shape = RoundedCornerShape(20.dp),
        border = BorderStroke(
            width = 1.dp,
            color = gray1
        )
    )
}

@Composable
private fun Rectangle() =
    Spacer(
        modifier = Modifier
            .fillMaxWidth()
            .height(1.dp)
            .background(gray2)
    )

@Composable
private fun ItemCard(title: String, sum: Double) =
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .height(52.dp),
        content = {
            Text(
                text = title,
                textAlign = TextAlign.Start
            )
            Text(
                text = "$sum₽",
                textAlign = TextAlign.End
            )
        },
        verticalAlignment = Alignment.CenterVertically
    )

@Composable
private fun TitleCard(title: String) =
    Text(
        modifier = Modifier
            .padding(
                bottom = 16.dp
            )
            .border(
                width = 0.dp,
                color = white,
                shape = RoundedCornerShape(0.dp)
            ),
        text = title,
        style = TextStyle(
            fontSize = 20.sp,
            lineHeight = 23.sp,
            fontFamily = FontFamily(
                Font(R.font.roboto)
            ),
            fontWeight = FontWeight.Bold,
            color = black
        )
    )

@Composable
private fun RowScope.Text(text: String, textAlign: TextAlign) =
    Text(
        modifier = Modifier
            .fillMaxWidth()
            .weight(50f),
        text = text,
        style = TextStyle(
            fontSize = 16.sp,
            lineHeight = 18.75.sp,
            fontFamily = FontFamily(
                Font(R.font.roboto)
            ),
            fontWeight = FontWeight.Normal,
            color = black,
            textAlign = textAlign
        )
    )